using Engine;
using Game;

namespace RuthlessConquest {
    public class ModSettingsScreen : Screen {
        public ButtonWidget LobbyServersButton;
        public ButtonWidget DisplayOptimalResultOfShipSentButton;
        public ButtonWidget DisplayProductionRateOfPlanetsButton;
        public ButtonWidget DisplayPlayerStatisticsButton;
        public ButtonWidget DisplayMoreNoticesButton;
        public ButtonWidget FeedbackButton;
        public ButtonWidget BackButton;
        public const string fName = "RCModSettingsScreen";

        public ModSettingsScreen() {
            Children.Add(new BackgroundWidget());
            Children.Add(new BoidsWidget());
            Children.Add(new InterlaceWidget());
            StackPanelWidget rootPanel = new() {
                Direction = LayoutDirection.Vertical,
                Margin = new Vector2(20f, 0f),
                Children = {
                    new CanvasWidget { Size = new Vector2(0f, 10f) },
                    new CanvasWidget {
                        Size = new Vector2(float.PositiveInfinity, -1f),
                        Children = {
                            new LabelWidget {
                                Text = LanguageControl.Get(fName, "1"),
                                FontScale = 0.75f,
                                Color = Colors.Fore,
                                HorizontalAlignment = WidgetAlignment.Center
                            }
                        }
                    },
                    new CanvasWidget { Size = new Vector2(0f, 10f) }
                }
            };
            CanvasWidget settingsContainer = new() {
                Size = new Vector2(float.PositiveInfinity, float.PositiveInfinity), Children = { new RectangleWidget(), new InterlaceWidget() }
            };
            ScrollPanelWidget scrollPanel = new() { Direction = LayoutDirection.Vertical, Margin = new Vector2(2f, 2f) };
            StackPanelWidget settingsPanel = new() { Direction = LayoutDirection.Vertical };
            UniformSpacingPanelWidget lobbyServersPanel = new() {
                Margin = new Vector2(0f, 3f),
                Children = {
                    new LabelWidget {
                        Text = LanguageControl.Get(fName, "2"),
                        HorizontalAlignment = WidgetAlignment.Far,
                        VerticalAlignment = WidgetAlignment.Center,
                        Margin = new Vector2(20f, 0f)
                    }
                }
            };
            LobbyServersButton = new BevelledButtonWidget {
                Margin = new Vector2(20f, 0f),
                HorizontalAlignment = WidgetAlignment.Near,
                VerticalAlignment = WidgetAlignment.Center,
                Size = new Vector2(float.PositiveInfinity, 60f),
                Text = string.Format(LanguageControl.Get(fName, "3"), SettingsManager.LobbyServers.Count)
            };
            lobbyServersPanel.Children.Add(LobbyServersButton);
            settingsPanel.Children.Add(lobbyServersPanel);
            UniformSpacingPanelWidget displayOptimalResultOfShipSentPanel = new() {
                Margin = new Vector2(0f, 3f),
                Children = {
                    new LabelWidget {
                        Text = LanguageControl.Get(fName, "6"),
                        HorizontalAlignment = WidgetAlignment.Far,
                        VerticalAlignment = WidgetAlignment.Center,
                        Margin = new Vector2(20f, 0f)
                    }
                }
            };
            DisplayOptimalResultOfShipSentButton = new BevelledButtonWidget {
                Text = SettingsManager.DisplayOptimalResultOfShipSent ? LanguageControl.Enable : LanguageControl.Disable,
                Margin = new Vector2(20f, 0f),
                HorizontalAlignment = WidgetAlignment.Near,
                VerticalAlignment = WidgetAlignment.Center,
                Size = new Vector2(float.PositiveInfinity, 60f)
            };
            displayOptimalResultOfShipSentPanel.Children.Add(DisplayOptimalResultOfShipSentButton);
            settingsPanel.Children.Add(displayOptimalResultOfShipSentPanel);
            UniformSpacingPanelWidget displayProductionRateOfPlanetsPanel = new() {
                Margin = new Vector2(0f, 3f),
                Children = {
                    new LabelWidget {
                        Text = LanguageControl.Get(fName, "7"),
                        HorizontalAlignment = WidgetAlignment.Far,
                        VerticalAlignment = WidgetAlignment.Center,
                        Margin = new Vector2(20f, 0f)
                    }
                }
            };
            DisplayProductionRateOfPlanetsButton = new BevelledButtonWidget {
                Text = SettingsManager.DisplayProductionRateOfPlanets ? LanguageControl.Enable : LanguageControl.Disable,
                Margin = new Vector2(20f, 0f),
                HorizontalAlignment = WidgetAlignment.Near,
                VerticalAlignment = WidgetAlignment.Center,
                Size = new Vector2(float.PositiveInfinity, 60f)
            };
            displayProductionRateOfPlanetsPanel.Children.Add(DisplayProductionRateOfPlanetsButton);
            settingsPanel.Children.Add(displayProductionRateOfPlanetsPanel);
            UniformSpacingPanelWidget displayPlayerStatisticsPanel = new() {
                Margin = new Vector2(0f, 3f),
                Children = {
                    new LabelWidget {
                        Text = LanguageControl.Get(fName, "8"),
                        HorizontalAlignment = WidgetAlignment.Far,
                        VerticalAlignment = WidgetAlignment.Center,
                        Margin = new Vector2(20f, 0f)
                    }
                }
            };
            DisplayPlayerStatisticsButton = new BevelledButtonWidget {
                Text = SettingsManager.DisplayPlayerStatistics ? LanguageControl.Enable : LanguageControl.Disable,
                Margin = new Vector2(20f, 0f),
                HorizontalAlignment = WidgetAlignment.Near,
                VerticalAlignment = WidgetAlignment.Center,
                Size = new Vector2(float.PositiveInfinity, 60f)
            };
            displayPlayerStatisticsPanel.Children.Add(DisplayPlayerStatisticsButton);
            settingsPanel.Children.Add(displayPlayerStatisticsPanel);
            UniformSpacingPanelWidget displayMoreNoticesPanel = new() {
                Margin = new Vector2(0f, 3f),
                Children = {
                    new LabelWidget {
                        Text = LanguageControl.Get(fName, "9"),
                        HorizontalAlignment = WidgetAlignment.Far,
                        VerticalAlignment = WidgetAlignment.Center,
                        Margin = new Vector2(20f, 0f)
                    }
                }
            };
            DisplayMoreNoticesButton = new BevelledButtonWidget {
                Text = SettingsManager.DisplayMoreNotices ? LanguageControl.Enable : LanguageControl.Disable,
                Margin = new Vector2(20f, 0f),
                HorizontalAlignment = WidgetAlignment.Near,
                VerticalAlignment = WidgetAlignment.Center,
                Size = new Vector2(float.PositiveInfinity, 60f)
            };
            displayMoreNoticesPanel.Children.Add(DisplayMoreNoticesButton);
            settingsPanel.Children.Add(displayMoreNoticesPanel);
            UniformSpacingPanelWidget feedbackPanel = new() {
                Margin = new Vector2(0f, 3f),
                Children = {
                    new LabelWidget {
                        Text = LanguageControl.Get(fName, "4"),
                        HorizontalAlignment = WidgetAlignment.Far,
                        VerticalAlignment = WidgetAlignment.Center,
                        Margin = new Vector2(20f, 0f)
                    }
                }
            };
            FeedbackButton = new BevelledButtonWidget {
                Text = LanguageControl.Get(fName, "5"),
                Margin = new Vector2(20f, 0f),
                HorizontalAlignment = WidgetAlignment.Near,
                VerticalAlignment = WidgetAlignment.Center,
                Size = new Vector2(float.PositiveInfinity, 60f)
            };
            feedbackPanel.Children.Add(FeedbackButton);
            settingsPanel.Children.Add(feedbackPanel);
            scrollPanel.Children.Add(settingsPanel);
            settingsContainer.Children.Add(scrollPanel);
            rootPanel.Children.Add(settingsContainer);
            rootPanel.Children.Add(new CanvasWidget { Size = new Vector2(0f, 20f) });
            BackButton = new BevelledButtonWidget {
                Text = LanguageControl.Back, Size = new Vector2(200f, 60f), Sound = "RCSounds/Click2", HorizontalAlignment = WidgetAlignment.Center
            };
            rootPanel.Children.Add(BackButton);
            rootPanel.Children.Add(new CanvasWidget { Size = new Vector2(0f, 10f) });
            Children.Add(rootPanel);
            scrollPanel.ScrollPosition = 0f;
        }

        public override void Enter(object[] parameters) {
            LobbyServersButton.Text = string.Format(LanguageControl.Get(fName, "3"), SettingsManager.LobbyServers.Count);
            DisplayOptimalResultOfShipSentButton.Text = SettingsManager.DisplayOptimalResultOfShipSent
                ? LanguageControl.Enable
                : LanguageControl.Disable;
            DisplayProductionRateOfPlanetsButton.Text = SettingsManager.DisplayProductionRateOfPlanets
                ? LanguageControl.Enable
                : LanguageControl.Disable;
            DisplayPlayerStatisticsButton.Text = SettingsManager.DisplayPlayerStatistics ? LanguageControl.Enable : LanguageControl.Disable;
            DisplayMoreNoticesButton.Text = SettingsManager.DisplayMoreNotices ? LanguageControl.Enable : LanguageControl.Disable;
        }

        public override void Update() {
            if (LobbyServersButton.IsClicked) {
                DialogsManager.ShowDialog(
                    this,
                    new EditLobbyServersDialog(count => LobbyServersButton.Text = string.Format(LanguageControl.Get(fName, "3"), count))
                );
            }
            if (DisplayOptimalResultOfShipSentButton.IsClicked) {
                SettingsManager.DisplayOptimalResultOfShipSent = !SettingsManager.DisplayOptimalResultOfShipSent;
                DisplayOptimalResultOfShipSentButton.Text = SettingsManager.DisplayOptimalResultOfShipSent
                    ? LanguageControl.Enable
                    : LanguageControl.Disable;
            }
            if (DisplayProductionRateOfPlanetsButton.IsClicked) {
                SettingsManager.DisplayProductionRateOfPlanets = !SettingsManager.DisplayProductionRateOfPlanets;
                DisplayProductionRateOfPlanetsButton.Text = SettingsManager.DisplayProductionRateOfPlanets
                    ? LanguageControl.Enable
                    : LanguageControl.Disable;
            }
            if (DisplayPlayerStatisticsButton.IsClicked) {
                SettingsManager.DisplayPlayerStatistics = !SettingsManager.DisplayPlayerStatistics;
                DisplayPlayerStatisticsButton.Text = SettingsManager.DisplayPlayerStatistics ? LanguageControl.Enable : LanguageControl.Disable;
            }
            if (DisplayMoreNoticesButton.IsClicked) {
                SettingsManager.DisplayMoreNotices = !SettingsManager.DisplayMoreNotices;
                DisplayMoreNoticesButton.Text = SettingsManager.DisplayMoreNotices ? LanguageControl.Enable : LanguageControl.Disable;
            }
            if (FeedbackButton.IsClicked) {
                WebBrowserManager.LaunchBrowser("https://gitee.com/Xiaofengdizhu/RuthlessConquestForSCAPI/issues");
            }
            if (Input.Back
                || BackButton.IsClicked) {
                ScreensManager.SwitchScreen(ScreensManager.PreviousScreen);
            }
        }
    }
}